#include <stdio.h>
#include <stdlib.h>

#define ERROR NULL
typedef int ElementType;
typedef struct LNode *PtrToLNode;
struct LNode {
    ElementType Data;
    PtrToLNode Next;
};
typedef PtrToLNode Position;
typedef PtrToLNode List;

Position FindLinkedList( List L, ElementType X );
List InsertLinkedList( List L, ElementType X, Position P );
List DeleteLinkedList( List L, Position P );
//
//int main()
//{
//    List L;
//    ElementType X;
//    Position P, tmp;
//    int N;
//
//    L = NULL;
//    scanf("%d", &N);
//    while ( N-- ) {
//        scanf("%d", &X);
//        L = Insert(L, X, L);
//        if ( L==ERROR ) {
//            printf("Wrong Answer\n");
//        }
//    }
//    scanf("%d", &N);
//    while ( N-- ) {
//        scanf("%d", &X);
//        P = Find(L, X);
//        if ( P == ERROR )
//            printf("Finding Error: %d is not in.\n", X);
//        else {
//            L = Delete(L, P);
//            printf("%d is found and deleted.\n", X);
//            if ( L==ERROR )
//                printf("Wrong Answer or Empty List.\n");
//        }
//    }
//    L = Insert(L, X, NULL);
//    if ( L==ERROR ) printf("Wrong Answer\n");
//    else
//        printf("%d is inserted as the last element.\n", X);
//    P = (Position)malloc(sizeof(struct LNode));
//    tmp = Insert(L, X, P);
//    if ( tmp!=ERROR ) printf("Wrong Answer\n");
//    tmp = Delete(L, P);
//    if ( tmp!=ERROR ) printf("Wrong Answer\n");
//    for ( P=L; P; P = P->Next ){
//        printf("%d ", P->Data);
//    }
//    return 0;
//}

/* 你的代码将被嵌在这里 */

Position FindLinkedList( List L, ElementType X ){
    List p =  L;
    while (p != NULL){
        if (p->Data == X){
            return p;
        }
        p = p ->Next;
    }
    return ERROR;
}

List InsertLinkedList(List L, ElementType X, Position P ){
    List tmp = (List)malloc(sizeof(List));
    tmp->Data=X;
    tmp->Next=NULL;
    if(L==P){
        tmp->Next=L;
        return tmp;
    }
    List head=L;
    while (L != NULL){
        if (L->Next ==  P){
            tmp->Next=L->Next;
            L->Next=tmp;
            return head;
        }
        L = L ->Next;
    }
    printf("Wrong Position for Insertion\n");
    return ERROR;

}

List DeleteLinkedList( List L, Position P ){
    if (L == P){
        L = L->Next;
        return L;
    }
    List p =  (List)malloc(sizeof(struct LNode));
    p -> Next = L;
    List pre = p;
    while (pre->Next != NULL){
        if (pre->Next ==  P){
            break;
        }
        pre = pre ->Next;
    }
    if (pre->Next == NULL){
        printf("Wrong Position for Deletion\n");
        return ERROR;
    }
    pre->Next=P->Next;
    P->Next=NULL;
    free(P);
    return p->Next;
}